<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 后台专用接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class VueAdmin extends Api
{

    public function getRules()
    {
        return array(
            'get_menu'       => array(),
            'send_weixin' => array(
                "text" => array( "name"=> "text"),
                "key" => array( "name"=> "key") 
            )
        );
    }

    public function send_weixin()
    {
        //这里是提示的开始
        $curl = new \PhalApi\CUrl();
        // 设置请求的头部信息。
        $curl->setHeader(array('Content-Type' => 'application/json'));
        // 设置curl选项
        $postDate = array(
            "msgtype" => "markdown",
            "markdown" => array(
                "content" => $this->text
            )
        );
        $key = $this->key;
        $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$key";
        return $curl->post($url, json_encode($postDate),2000);
    }
    
    
    /**
     * 菜单获取
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function get_menu()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        $table = "admin_portal_menu";
        $where = array(
            "sort_num" => 1
        );

        $One = \PhalApi\DI()->notorm->$table->where($where)->order('orders ASC')->fetchAll();
        if ( $One ){

            $res = array();
            foreach($One as $k=>$v){
                $res[$k] = array(
                    'path' => $v['path'],
                    'component' => $v['component'],
                    'redirect' => $v['redirect'],
                    'name' => $v['name'],
                    'meta' => array(
                        'title' => $v['title'],
                        'icon' => $v['icon'],
                        'roles' => json_decode($v['assign_admin_roles'], true),
                        'hidden' => $v['hidden'],
                    ),
                );
                $children = \PhalApi\DI()->notorm->$table->where(array("parent_id"=>$v['id']))->order('orders ASC')->fetchAll();
                foreach($children as $k1=>$v1){
                    $res[$k]['children'][] = array(
                        'path' => $v1['path'],
                        'component' => $v1['component'],
                        'name' => $v1['name'],
                        'meta' => array(
                            'title' => $v1['title'],
                            'icon' => $v1['icon'],
                            'roles' => json_decode($v1['assign_admin_roles'], true),
                        ),
                    );
                }
     
            }
            return $res;
        }
        

    }

}
